import Mock, { Random } from 'mockjs'
import { MockMethod } from 'vite-plugin-mock'

const {list} = Mock.mock({
  'list|123': [{
      'id|+1': 1,
      name: '@ctitle(2, 4)',
      idCard: Random.id(),
      phone: '@string("number", 11)',
      serviceStatus: Math.random() > 0.5 ? '待服务' : '待审核',
      servicePackage: '@cword(5)' + '包',
      serviceItems: '@ctitle(2, 4)',
      time: '@datetime("yyyy-MM-dd HH:mm:ss")',
      doctor: '@ctitle(2)'
  }]
})

let _id = 124

export default [
    {
      url: '/api/v1/service',
      method: 'get',
      response: ({ query: {nextIndex = 0} }) => {
        const total = list.length
        const pageSize = 10

        nextIndex = Number(nextIndex)

        const data = Mock.mock({
          list: list.slice(nextIndex, nextIndex + pageSize),
          total,
          nextIndex: nextIndex + pageSize,
          isEnd: nextIndex + pageSize >= total
        })
        return {
          code: 200,
          data,
        }
      },
    },
    {
      url: '/api/v1/service',
      method: 'post',
      response: ({ body }) => {
        
        list.push({
          ...body,
          id: _id++,
        })

        return {
          code: 200,
          data: {
            status: 200,
            message: '添加成功!',
          },
        }
      },
    },
    {
      url: '/api/v1/service',
      method: 'put',
      response: ({ body }) => {
        // const olddata = list.find((item => item.id === body.id))
        // const data = body

        list.forEach(item => {
          if (item.id === body.id) {
            Object.assign(item, body)
          }
        })

        return {
          code: 200,
          data: {
            status: 200,
            message: '修改成功!'
          },
        }
      },
    },
    {
      url: '/api/service',
      method: 'get',
      response: ({query: {id}}) => {
        id = Number(id)
        const data = list.find(item => item.id === id)
        
        return {
          code: 200,
          data
        }
      },
    }
  ] as MockMethod[]